Solutions for Comp248 Assignment 4

#include<iostream.h>
#include"Invoice.h"

//class implementation
//the constructor implementation
Invoice::Invoice(String Number, String purchase, double Cost,int Qty,char tax)
{

	invoiceNum = Number;
	item = purchase;
	unitPrice = Cost;

	if(Qty<0)
		{
			Qty=0;
			Quantity=Qty;
		}
	else
		{
			Quantity=Qty;
		}

	//if the tax is anything other than T,B or N then Type T is taken as default.
	if((tax!='T')&&(tax!='B')&&(tax!='N'))
		{
			cout<<" The tax is taken as 'T' by default!! "<<endl;
			tax='T';
			taxType=tax;
		}		
	else
		{
			taxType=tax;
		}
}



//this function retruns the price before tax!!
double Invoice::totalPrice()
{
	return unitPrice*Quantity;
	
}



//this function returns the GST part of the tax
double Invoice::calculatorGST()
{
	double GST;
	switch(taxType)
	{
		case'T':
			GST=  totalPrice()*0.07;			
		break;

		case'B':
			GST=  totalPrice()*0.07;		
		break;

		case'N':
			GST= 0;
		break;		
	}

	return GST;
}



//this function returns PST part of the tax
double Invoice::calculatorPST()
{
	double PST;
	switch(taxType)
	{
    	case'T':
			PST= (totalPrice() + calculatorGST())*0.075;						
		break;

		case'B':
			PST= 0;		
		break;

		case'N':
			PST= 0;
		break;	
	}

	return PST;
}



//this function will return the shipping cost
double Invoice::shipCost()
{
	double temp = totalPrice()/10;
	int convertor = int (temp);
	double charge= 5.0+(0.25*convertor);
	return charge;	
}



//this function will return the total cost of everything
double Invoice::grandTotal()
{
	return totalPrice()+calculatorGST()+calculatorPST()+shipCost();

}




// this function outputs Invoice information 
void Invoice::writeReport() 
{
   cout <<" Invoice "<<"#"<<invoiceNum<< endl;
   cout <<" Item:   "<<item<< endl;
   cout <<" Qty:    "<<Quantity<< endl;
   cout <<" Unit Price:  $"<<unitPrice<< endl;
   cout <<" Price:                    $"<<setreal(1,2)<<totalPrice()<<endl;
   cout <<" GST:                      $"<<setreal(1,2)<<calculatorGST()<<endl;
   cout <<" PST:                      $"<<setreal(1,2)<<calculatorPST()<<endl;
   cout <<" Shipping:                 $"<<setreal(1,2)<<shipCost()<<endl;
   cout <<" Total:                    $"<<setreal(1,2)<<grandTotal()<<endl;
   cout<<endl;
}


//main program
int main()
{
	
	//Testing all the various possibilty to ensure that the class can handle
	//various constructor declarations.

	Invoice test1("3746578","Winter boots",139.95,2,'T');
	//Invoice(String Number, String purchase, double Cost,int Qty,char tax)

	test1.writeReport();

	Invoice test2("3746578","Winter boots",139.95,2,'B');
	//Invoice(String Number, String purchase, double Cost,int Qty,char tax)

	test2.writeReport();


	Invoice test3("3746578","Winter boots",139.95,2,'N');
	//Invoice(String Number, String purchase, double Cost,int Qty,char tax)

	test3.writeReport();


	Invoice test4("5352765","Winter Jackets",900.89,4,'G');
	//Invoice(String Number, String purchase, double Cost,int Qty,char tax)

    test4.writeReport();

	Invoice test5("3746578","Nike Shoes",100.0,1,'N');
	//Invoice(String Number, String purchase, double Cost,int Qty,char tax)

	test5.writeReport();

	return 0;
}

//RUN:

/*
 Invoice #3746578
 Item:   Winter boots
 Qty:    2
 Unit Price:  $139.95
 Price:                    $279.90
 GST:                      $19.59
 PST:                      $22.46
 Shipping:                 $11.75
 Total:                    $333.70

 Invoice #3746578
 Item:   Winter boots
 Qty:    2
 Unit Price:  $139.95
 Price:                    $279.90
 GST:                      $19.59
 PST:                      $0.00
 Shipping:                 $11.75
 Total:                    $311.24

 Invoice #3746578
 Item:   Winter boots
 Qty:    2
 Unit Price:  $139.95
 Price:                    $279.90
 GST:                      $0.00
 PST:                      $0.00
 Shipping:                 $11.75
 Total:                    $291.65

 The tax is taken as 'T' by default!!
 Invoice #5352765
 Item:   Winter Jackets
 Qty:    4
 Unit Price:  $900.89
 Price:                    $3603.56
 GST:                      $252.25
 PST:                      $289.19
 Shipping:                 $95.00
 Total:                    $4239.99

 Invoice #3746578
 Item:   Nike Shoes
 Qty:    1
 Unit Price:  $100
 Price:                    $100.00
 GST:                      $0.00
 PST:                      $0.00
 Shipping:                 $7.50
 Total:                    $107.50

Press any key to continue
*/